/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.meteocal.EJB.sessionBeans;

import it.polimi.meteocal.EJB.entities.Event;
import it.polimi.meteocal.EJB.entities.Invite;
import it.polimi.meteocal.EJB.entities.InviteNotification;
import it.polimi.meteocal.EJB.entities.User;
import it.polimi.meteocal.EJB.gui.EventBean;
import it.polimi.meteocal.EJB.gui.UserBean;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Fabio
 */
@Stateless
public class InviteManager {
    
    @PersistenceContext 
    private EntityManager em;
   
    public Invite find(int id){
        return em.find(Invite.class, id);
    }
    
    public Invite findbyEventAndGuest(String a, int id){
        Query q= em.createNativeQuery("SELECT id FROM Invite WHERE (guest = '" + a + "') AND (event = '" + id +"')");
        List<Long> allinv = q.getResultList();
        int [] iid = new int[1];
        for (Long iDd : allinv) {
            Integer ad = iDd.intValue();
            int ID = ad.intValue();
            iid[0]= ID;
        }
        return find(iid[0]);
    }
    
    public List<Invite> checkInvites(String a, int id){
        Query q= em.createNativeQuery("SELECT id FROM Invite WHERE (guest = '" + a + "') AND (event = '" + id +"')");
        List<Long> allinv = q.getResultList();
        List<Invite> invs = new ArrayList<Invite>();
        for (Long iDd : allinv) {
            Integer ad = iDd.intValue();
            int ID = ad.intValue();
            Invite i = find(ID);
            invs.add(i);
        }
        return invs;
    }
    
   public void create (String s, int id){
       User u= em.find(User.class, s);
       Event e= em.find(Event.class, id);
       Invite newInvite = new Invite();
       newInvite.setEvent(e);
       newInvite.setGuest(u);
       newInvite.setId(1);
       List<Invite> il= e.getInviteList();
       il.add(newInvite);
       e.setInviteList(il);
       List<Event> el= u.getEventList();
       el.add(e);
       u.setEventList(el);
       em.persist(newInvite);
       InviteNotification in= new InviteNotification();
       in.setInvite(newInvite);
       in.setReceiverUser(u);
       in.setId(1);
       Date d = new Date();
       in.setRecDate(d);
       em.persist(in);
   }
       
   public void confirm(User u, Event e, int b){
       String query = "UPDATE invite SET accept = '" + b + "' WHERE (guest = '" + u.getUsername() +"' AND event = '" + e.getId() +"' ) ";
       Query q = em.createNativeQuery(query);
       q.executeUpdate();
   }
   
   public void delete(User u, Event e){
       Invite i = findbyEventAndGuest(u.getUsername(), e.getId());
       em.remove(i);
   }
   
}